package com.example.demo.controller;

import com.example.demo.entity.Hotel;
import com.example.demo.service.HotelService;

import com.example.demo.vo.HotelVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/hotel")
public class HotelController {

    private HotelService hotelService;

    //上传后定义的新的图片名
    String imageName = "";

    //标志位（是否经过图片删除）
    boolean flag = false;

    @Autowired
    public void setHotelService(HotelService hotelService) {
        this.hotelService = hotelService;
    }

    //获取所有酒店信息
    @RequestMapping("/getAll")
    public List<Hotel> getAllHotel() {
        List<Hotel> list = hotelService.getAllHotel();
        return list;
    }

    //添加酒店
    @RequestMapping("/add")
    public String addHotel(Hotel hotel) {
        if(hotel.getHotelName() != null && !("".equals(hotel.getHotelName()))){
            //设置对象images属性
            hotel.setImages(imageName);
            imageName = "";
            //添加对象
            int count = hotelService.addHotel(hotel);
            if(count > 0){
                return "ok";
            }else {
                return "error";
            }
        }else{
            return "error";
        }

    }

    //修改酒店信息
    @RequestMapping("/update")
    public String updateHotel(Hotel hotel){
        //设置对象images属性
        if(flag){
            hotel.setImages(imageName);
            imageName = "";
            flag = false;
        }
        int count = hotelService.updateHotel(hotel);
        if(count > 0){
            return "ok";
        }else {
            return "error";
        }
    }

    //删除酒店
    @RequestMapping("/delete")
    public String deleteHotel(@RequestParam("id") Integer id){
        int count = hotelService.deleteHotel(id);
        if(count > 0){
            return "ok";
        }else{
            return "error";
        }
    }

    //模糊查询
    @RequestMapping("/select")
    public PageInfo selectHotel(HotelVO hotelVO,@RequestParam(value = "pageNo",defaultValue = "0") int pageNo, @RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
        PageInfo pageInfo = hotelService.selectHotel(hotelVO,pageNo,pageSize);
        return pageInfo;
    }

    //分页
    @RequestMapping("/getPage")
    public PageInfo getPageInfo(@RequestParam("pageNo") int pageNo, @RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
        PageInfo pageInfo = hotelService.getPageInfo(pageNo, pageSize);
        return pageInfo;
    }


    //上传图片方法
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest request){
        if(file.isEmpty()){
            return "error";
        }
        //图片路径
        String realPath = System.getProperty("user.dir") + "/src/main/resources/static/images/";
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        //拿到图片名
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        try {
            file.transferTo(new File(folder, newName));
            //图片名
            imageName += newName;
            return "ok";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }

    //删除图片
    @RequestMapping("/deleteImage")
    public void deleteImage(Integer hotelId){
        //获取酒店展示图片的名字
        String name = hotelService.getImageName(hotelId);
        new File("E:/IDEA/LanQiao/Travel/travel/src/main/resources/static/images/"+name).delete();
        //修改标志位
        flag = true;
    }


}
